package rx.fxa.com.carousel;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;

import me.relex.circleindicator.CircleIndicator;

/**
 * Created by fengxiang on 2016/9/29.
 */
public class CarouselFragment extends Fragment {

    static OnItemClickListener onItemClickListener = null;
    View rootView;

    CarouselViewPager pager;
    CircleIndicator indicator;
    ArrayList<ImageItem> items;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.root_layout, container, false);
        pager = (CarouselViewPager) rootView.findViewById(R.id.viewPager);
        indicator = (CircleIndicator) rootView.findViewById(R.id.indicator);
        int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, width / 16 * 9);
        pager.setLayoutParams(params);
        items = new ArrayList<>();
        Bundle bundle = getArguments();
        items.addAll((ArrayList<ImageItem>) bundle.get("data"));
        pager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        indicator.setViewPager(pager);
        return rootView;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(ArrayList<ImageItem> data) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", data);
        setArguments(bundle);
    }

    class MyPagerAdapter extends FragmentPagerAdapter {


        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public ImageFragment getItem(int position) {
            return ImageFragment.getInstance(items.get(position));
        }

        @Override
        public int getCount() {
            return items.size();
        }
    }

    static class ImageFragment extends Fragment {


        ImageItem imageItem;

        public ImageFragment(ImageItem imageItem) {
            this.imageItem = imageItem;
        }

        public static ImageFragment getInstance(ImageItem imageItem) {
            return new ImageFragment(imageItem);
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            SimpleDraweeView draweeView = new SimpleDraweeView(getActivity());
            draweeView.setScaleType(ImageView.ScaleType.FIT_XY);
            draweeView.setImageURI(Uri.parse(imageItem.imgUrl));
            draweeView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(imageItem);
                }
            });
            return draweeView;
        }
    }


}
